from typing import Union
from fastapi import FastAPI,Path,Query

app = FastAPI()

'''
4.1 Path 使用
'''
@app.get("/items1/{item_id}")
async def read_items(
    item_id: int = Path(title="The ID of the item to get"),
):
    return {"item_id": item_id}


'''
4.2 按需对参数排序
'''
@app.get("/items2/{item_id}")
async def read_items(
        *,
        q: str,
        item_id: int = Path(title="The ID of the item to get"),
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    return results

'''
4.3 通过 Path 校验
'''
'''数值校验：大于等于'''
@app.get("/items3/{item_id}")
async def read_items(
    *, item_id: int = Path(title="The ID of the item to get", ge=1), q: str
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    return results


'''数值校验：大于和小于等于'''
@app.get("/items4/{item_id}")
async def read_items(
    *,item_id: int = Path(title="The ID of the item to get", gt=0, le=1000),q: str,
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    return results


'''浮点数、大于和小于'''
@app.get("/items5/{item_id}")
async def read_items(
    *,
    item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
    q: str,
    size: float = Query(gt=0, lt=10.5),
):
    results = {"item_id": item_id,"size":size}
    if q:
        results.update({"q": q})
    return results



if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)